﻿package fab.buttons
{	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.Event;

	/**
	var btn:Bouton = new Bouton(mc as MovieClip);
	btn.activated
	btn.clicable
	btn.clicked_frame
	btn.rollover_frame
	 */	
	public class Bouton extends Sprite
	{
		public var activated:Boolean = false;
		public var clicable:Boolean = true;
		public var clicked_frame:int = 10;
		public var rollover_frame:int = 10;
		
		private var targetFrame:int = 0;
		private var btn:MovieClip;
		
		public function Bouton( btn:MovieClip)
		{
			this.btn = btn;
			addChild( btn );
			btn.gotoAndStop( 1 );
			buttonMode = true;
			btn.addEventListener(MouseEvent.MOUSE_OVER, _survole );
		}
		private function _framing( event:Event ):void
		{
			var btn:MovieClip = ( event.currentTarget as MovieClip );
			if ( targetFrame == 0 ) return;
			if ( btn.currentFrame == targetFrame ) btn.removeEventListener( Event.ENTER_FRAME, _framing );
			if ( btn.currentFrame > targetFrame ) btn.prevFrame();
			if ( btn.currentFrame < targetFrame ) btn.nextFrame();
		}	
		private function _survole(event:MouseEvent):void
		{
			if ( activated ) return;
			targetFrame = rollover_frame;
			btn.addEventListener( Event.ENTER_FRAME, _framing );
			btn.removeEventListener(MouseEvent.MOUSE_OVER, _survole );
			btn.addEventListener(MouseEvent.MOUSE_OUT, _sort );
		}
		private function _sort(event:MouseEvent):void
		{
			targetFrame = activated?clicked_frame:1;
			btn.addEventListener( Event.ENTER_FRAME, _framing );
			btn.addEventListener(MouseEvent.MOUSE_OVER, _survole );
			btn.removeEventListener(MouseEvent.MOUSE_OUT, _sort );
		}
		private function _relache(event:MouseEvent):void
		{
			btn.gotoAndStop( activated?clicked_frame:1 );
			btn.removeEventListener(MouseEvent.MOUSE_UP, _relache );
		}
	}
}